package com.bmp.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bmp.model.Bug;
import com.bmp.model.User;
import com.bmp.model.vo.BugVoProModUser;
import com.bmp.service.BugManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class BugAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	private BugManager bugManager;
	private Bug bug;
	
	private JSONObject jsonObject;
	private String page;
	private String rows;
	
	//初始化bugManager
	public BugAction(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		bugManager = (BugManager) ctx.getBean("bugManager");
	}
	
	//显示bug信息，json格式
	public String bugInfo(){
		//当前页  
        int intPage = Integer.parseInt((page == null || page == "0") ? "1":page);  
        //每页显示条数          
        int number = Integer.parseInt((rows == null || rows == "0") ? "10":rows);  
        //每页的开始记录  第一页为1  第二页为number +1   
        int start = (intPage-1)*number;  
        List<BugVoProModUser> bugList = bugManager.getBug(start, number);//每页的数据，放入list        
        Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
        jsonMap.put("total", bugManager.getTotalPages());//total键 存放总记录数，必须的     
        jsonMap.put("rows", bugList);//rows键 存放每页记录 list  
        jsonObject = JSONObject.fromObject(jsonMap);//格式化result,一定要是JSONObject  
		return "bugInfo";
	}
	
	//添加bug信息
	public String addBug(){
		bugManager.addBug(this.bug);
		return "success";
	}
	
	//删除bug
	public String deleteBug(){
		bugManager.deleteBugById(this.bug.getId());
		return "success";
	}
	
	//更新bug信息
	public String updateBug(){
		bugManager.updateBug(this.bug);
		return "updateBug";
	}
	
	//通过Bug提交人ID获取Bug信息
	public String getBugsByUserId(){
		User user = (User)ActionContext.getContext().getSession().get("user");
		List<Bug> bugs = bugManager.getBugByUserId(user.getId());
		ActionContext.getContext().getSession().put("myBugs", bugs);
		return "success";
	}
	//通过Bug指派人ID获取Bug信息
	public String getBugsByAppointId(){
		User user = (User)ActionContext.getContext().getSession().get("user");
		List<Bug> bugs = bugManager.getBugByAppointId(user.getId());
		ActionContext.getContext().getSession().put("AppointBug", bugs);
		return "success";
	}
	//通过Bug提交人ID获取Bug总数
	public String getBugTotleByUserId(){
		bugManager.getBugTotleByUserId(this.bug.getFinder_id());
		return "success";
	}
	
	//根据项目ID获取Bug信息
	public String getBugByProId(){
		bugManager.getBugByProjectId(this.bug.getPro_id());
		return "success";
	}
	//根据项目ID获取Bug总数
	public String getBugTotleByProId(){
		bugManager.getBugTotleByProId(this.bug.getPro_id());
		return "success";
	}
	
	//根据模块ID获取Bug信息
	public String getBugByModId(){
		bugManager.getBugByModId(this.bug.getMode_id());
		return "success";
	}
	
	//根据模块ID获取Bug总数
	public String getBugTotleByModId(){
		bugManager.getBugTotleByModId(this.bug.getMode_id());
		return "success";
	}
	/******************getter and setter*******************/
	public BugManager getBugManager() {
		return bugManager;
	}
	public void setBugManager(BugManager bugManager) {
		this.bugManager = bugManager;
	}
	public Bug getBug() {
		return bug;
	}
	public void setBug(Bug bug) {
		this.bug = bug;
	}
	public JSONObject getJsonObject() {
		return jsonObject;
	}
	public void setJsonObject(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
	}
	public String getPage() {
		return page;
	}
	public void setPage(String page) {
		this.page = page;
	}
	public String getRows() {
		return rows;
	}
	public void setRows(String rows) {
		this.rows = rows;
	}
}
